package com.nikitova.craps3d.view.components.game3d 
{
	import jiglib.math.JNumber3D;
	import jiglib.physics.PhysicsSystem;
	import jiglib.physics.RigidBody;
	import jiglib.plugin.papervision3d.Papervision3DPhysics;

	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.layer.ViewportLayer;
	import org.papervision3d.view.layer.util.ViewportLayerSortMode;
	import org.papervision3d.view.stats.StatsView;

	import flash.display.MovieClip;
	import flash.events.Event;

	/**
	 * @author schranz
	 */
	public class GameView3D extends BasicView 
	{		
		
		private var _physics 					: Papervision3DPhysics;
		private var _light 						: PointLight3D;
		private var _vplGrass 					: ViewportLayer;
		private var _vplTable 					: ViewportLayer;
		private var _vplChips 					: ViewportLayer;
		private var _vplDices					: ViewportLayer;
		private var lines:Lines3D;

		public function GameView3D(viewportWidth : Number = 640, viewportHeight : Number = 480,scaleToStage : Boolean = true, interactive : Boolean = true) 
		{
			super(viewportWidth, viewportHeight, scaleToStage, interactive);
			initPaperVision3D();
			initJiglib();
		}

		public function createGameView3D(widthRoom : Number, heightRoom : Number, lengthRoom : Number, material : MovieClip) : void
		{
			
		}

		public function createStats() : void
		{
			var stats : StatsView = new StatsView(renderer);
			addChild(stats);
		}
		
		public function createAxis() : void
		{
			lines = new Lines3D();
			scene.addChild(lines);
			var xLine:Line3D = new Line3D(lines,new LineMaterial(0xFF0000,1),1,new Vertex3D(-300,0,0),new Vertex3D(300,0,0));
			lines.addLine(xLine);
			var yLine:Line3D = new Line3D(lines,new LineMaterial(0x00FF00,1),1,new Vertex3D(0,-300,0),new Vertex3D(0,300,0));
			lines.addLine(yLine);
			var zLine:Line3D = new Line3D(lines,new LineMaterial(0x0000FF,1),1,new Vertex3D(0,0,-300),new Vertex3D(0,0,300));
			lines.addLine(zLine);
		}

		private function initPaperVision3D() : void 
		{		
			camera.focus 	= 20;
			camera.far 		= 1000;
			camera.z = -500;
			camera.y = 300;
			
			
			_light 			= new PointLight3D(true, true);
			
			viewport.containerSprite.sortMode 	= ViewportLayerSortMode.INDEX_SORT;
			
			_vplGrass					= new ViewportLayer(viewport, null);
			_vplGrass.layerIndex 		= 1;
			_vplGrass.sortMode 			= ViewportLayerSortMode.Z_SORT;
			
			
			_vplTable					= new ViewportLayer(viewport, null);
			_vplTable.layerIndex 		= 2;
			_vplTable.sortMode 			= ViewportLayerSortMode.Z_SORT;
			
			
			_vplChips 					= new ViewportLayer(viewport, null);
			_vplChips.layerIndex 			= 3;
			_vplChips.sortMode 			= ViewportLayerSortMode.Z_SORT;
			
			
			_vplDices 					= new ViewportLayer(viewport, null);
			_vplDices.layerIndex 			= 4;
			_vplDices.sortMode 			= ViewportLayerSortMode.Z_SORT;
			
			
			viewport.containerSprite.addLayer(_vplGrass);
			viewport.containerSprite.addLayer(_vplTable);
			viewport.containerSprite.addLayer(_vplChips);
			viewport.containerSprite.addLayer(_vplDices);	
		}

		private function initJiglib() : void 
		{
			// set normal mode physic engine
			PhysicsSystem.getInstance().setSolverType("NORMAL"); 
			_physics = new Papervision3DPhysics(scene, 1);
		}

		public function addBody(body:RigidBody,viewportLayer:ViewportLayer = null):void
		{
			scene.addChild(_physics.getMesh(body));
			if(viewportLayer)
		  	  viewportLayer.addDisplayObject3D(_physics.getMesh(body), true);
		}
		
		public function addDO3D(do3D:DisplayObject3D,viewportLayer:ViewportLayer = null):void
		{
			scene.addChild(do3D);
			if(viewportLayer)
		  	 viewportLayer.addDisplayObject3D(do3D, true);
		}


		public function setGravity(gravity : Number) : void
		{
			PhysicsSystem.getInstance().setGravity(JNumber3D.multiply(JNumber3D.UP, -gravity));
		}

		public function get light() : PointLight3D 
		{
			return _light;
		}

		public function get physics() : Papervision3DPhysics
		{
			return _physics;
		}
		
		public function get clothLayer() : ViewportLayer 
		{
			return _vplGrass;
		}
		public function get tableLayer() : ViewportLayer 
		{
			return _vplTable;
		}
		public function get chipsLayer() : ViewportLayer 
		{
			return _vplChips;
		}
		public function get diceLayer() : ViewportLayer 
		{
			return _vplDices;
		}

		protected override function onRenderTick(event : Event = null) : void 
		{

			PhysicsSystem.getInstance().integrate(0.6);
			super.onRenderTick(event);
		}
	}
}
